package com.lc.hello;

import com.lc.utils.InsertImageToPdf;
import com.lc.utils.PdfKeywordFinder;
import org.jodconverter.office.OfficeException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.jodconverter.DocumentConverter;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 这里的@RestController   相当于@ResponseBody + @Controller
 */
@RestController
public class HelloController {
    @Value("${pageSize}")
    private Integer pageSize;
    @Value("${devName}")
    private String devName;
    @Resource
    private DocumentConverter documentConverter;

    String keyword = "自我评价";
    String inPdfPath = "/root/Downloads/testfiles/pfile.pdf";
    String inPptPath = "/root/Downloads/testfiles/test.docx";

    String inPicPath = "/root/Downloads/testfiles/img-pdf.png";
    String outPdfPath = "/root/Downloads/testfiles/pfile-out.pdf";

//    String inPdfPath = "F:/testfiles/pfile.pdf";
//    String inPptPath = "F:/testfiles/test.docx";
//
//    String inPicPath = "F:/testfiles/img-pdf.png";
//    String outPdfPath = "F:/testfiles/pfile-out.pdf";


    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String helloSpringBoot() {
        return "Hello SpringBoot!";
    }

    @RequestMapping(value = "/showInfo", method = RequestMethod.GET)
    public String infoSpringBoot() {
        return "showInfo pageSize = " + pageSize + " devName = " + devName;
    }

    @RequestMapping(value = "/convertfiles", method = RequestMethod.GET)
    public String convertFiles() {
        String re = "convert files fail";

        File wfile = new File(inPptPath);
        File pfile = new File(inPdfPath);
        try {
            documentConverter.convert(wfile).to(pfile).execute();
            re = "convert files ok";
        } catch (OfficeException e) {
            e.printStackTrace();
        }

        return re;
    }

    @RequestMapping(value = "/addPic", method = RequestMethod.GET)
    public String addPic() {
        String re = "add pic fail";

        try {
            List<float[]> positions = null;

            positions = PdfKeywordFinder.findKeywordLastPostions(inPdfPath, "的");

            if (positions != null && positions.size() > 0) {

                List<InsertImageToPdf.InsertPicInfo> insertInfos = new ArrayList<>();
                for (float[] position : positions) {
                    System.out.print("pageNum: " + (int) position[0]);
                    re = "pageNum: " + (int) position[0];
                    System.out.print("\tx: " + position[1]);
                    re += "\tx: " + position[1];
                    System.out.println("\ty: " + position[2]);
                    re += "\ty: " + position[2];
                    System.out.println("\tcharWidth:" + position[3]);//每个字符的宽度
                    re += "\tcharWidth:" + position[3];

                    InsertImageToPdf.InsertPicInfo tempInfo = new InsertImageToPdf.InsertPicInfo();
                    tempInfo.imagePath = inPicPath;
                    tempInfo.insertPage = (int) position[0];
                    tempInfo.insertX = (int) position[1];
                    tempInfo.insertY = (int) position[2];
                    tempInfo.picWidth = 10;
                    tempInfo.picHeight = 10;

                    insertInfos.add(tempInfo);
                }
                InsertImageToPdf.signPdfByStampKeyNocertMultiplePics(inPdfPath, outPdfPath, insertInfos);
            }

            re = "add pic success";

        } catch (Exception e) {
            e.printStackTrace();
        }
        return re;
    }

    @RequestMapping(value = "/findWords", method = RequestMethod.GET)
    public String findWords() {
        String re = "find words fail";
        List<float[]> positions = null;
        try {
            positions = PdfKeywordFinder.findKeywordPostions(inPdfPath, keyword);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("total:" + positions.size());
        if (positions != null && positions.size() > 0) {
            for (float[] position : positions) {
                System.out.print("pageNum: " + (int) position[0]);
                re = "pageNum: " + (int) position[0];
                System.out.print("\tx: " + position[1]);
                re += "\tx: " + position[1];
                System.out.println("\ty: " + position[2]);
                re += "\ty: " + position[2];
                System.out.println("\tcharWidth:" + position[3]);//每个字符的宽度
                re += "\tcharWidth:" + position[3];
            }
        }

        return re;
    }
}